package com.gmail.blackdog1987.onedollar.dao;

import com.gmail.blackdog1987.onedollar.dao.hibernate.EntityHibernateDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.onedollar.entity.*;
import com.gmail.blackdog1987.onedollar.entity.base.Page;
import com.gmail.blackdog1987.onedollar.vo.GoodsJudgeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository("userChargeDao")
public class UserChargeDao extends EntityHibernateDao<TUserChargeEntity>{
    @Autowired
    BaseJdbcDao baseJdbcDao;
    public Page<TUserChargeEntity> queryForPage(int pageindex, int pagesize, int userid, int type){
        String whereSql;

        whereSql = " FROM t_user_charge WHERE user_id = ? ";

        if(type == 0){
            whereSql += " AND channel >= 10 ";
        }else if(type == 1){
            whereSql += " AND channel < 10 ";
        }
        whereSql += " ORDER BY createtime desc ";

        String selectionSql = " SELECT * ";

        String limit = " limit " + (pageindex-1)*pagesize + "," + pagesize;
        Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" SELECT COUNT(*) " + whereSql , new Object[]{userid});

        List<TUserChargeEntity> list = JdbcUtil.queryForList(baseJdbcDao, TUserChargeEntity.class, selectionSql + whereSql + limit,  new Object[]{userid});

        Page page = new Page(list, pageindex, totalCount, pagesize);

        return page;
    }
}
